<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:m3="http://www.openM3.org/m3"
    xmlns:op="http://www.ia.pw.edu.pl/m3/13node">

	<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
	<!-- keys -->
	<xsl:key name="offer_key" match="m3:Offer|m3:GroupingOffer" use="@id"/>
	<xsl:key name="comm_key" match="m3:Commodity" use="@id"/>
	<xsl:key name="ent_key" match="m3:MarketEntity" use="@id"/>
	<xsl:key name="arc_key" match="m3:arc" use="@id"/>
	<xsl:key name="node_key" match="m3:node" use="@id"/>
	<xsl:key name="cal_key" match="m3:CalendarPeriod" use="@id"/>
	<xsl:key name="ent_key" match="m3:MarketEntity" use="@id"/>
	<xsl:key name="prog_key" match="m3:Programme" use="@id"/>
	<xsl:key name="comm_dref" match="m3:Commodity" use="@dref"/>
	
	<!-- set item definition -->
	<xsl:template match="m3:Offer|m3:Commodity|m3:arc|m3:node|m3:CalendarPeriod|m3:MarketEntity|m3:Programme" mode="set">
		<xsl:call-template name="single_quote"/>
		<xsl:apply-templates select="." mode="name"/>
		<xsl:call-template name="single_quote"/>
	</xsl:template>
	
	<!-- generic set declaration -->
	<!-- $set_name - name of the set to be displayed -->
	<xsl:template name="generic_set_declaration">
		<xsl:param name="set_name"/>
		set <xsl:value-of select="$set_name"/>;<xsl:call-template name="newline"/>
	</xsl:template>
	
	<!-- grouping constraints definition - without time -->	
	<xsl:template name="grouping_constraints">
		<xsl:for-each select="//m3:offers/m3:GroupingOffer">
			<xsl:variable name="id_go" select="@id"/>
	
			<xsl:for-each select="key('comm_key', key('offer_key', $id_go)/m3:groups/m3:groupedCommodity/@ref)">
				<xsl:variable name="id_com" select="@id"/>
				<!-- minimum balance -->
				<xsl:if test="key('comm_key', $id_com)/@minBalance != '-INF'">
					subject to <xsl:call-template name="constr_name"><xsl:with-param name="c_name" select="concat('grouping_constr_', $id_com, '_min')"/></xsl:call-template> : <xsl:for-each select="key('offer_key', $id_go)/m3:groups[m3:groupedCommodity/@ref = $id_com]">
						<xsl:call-template name="coefficient">
							<xsl:with-param name="number" select="m3:groupedOffer/@coefficientValue"/>
						</xsl:call-template>*offer[<xsl:call-template name="single_quote"/><xsl:value-of select="m3:groupedOffer/@ref"/><xsl:call-template name="single_quote"/>] </xsl:for-each> &gt;= <xsl:value-of select="key('comm_key', $id_com)/@minBalance"/>;<xsl:call-template name="newline"/>
				</xsl:if>
				<!-- maximum balance -->
				<xsl:if test="key('comm_key', $id_com)/@maxBalance != '+INF'">
					subject to <xsl:call-template name="constr_name"><xsl:with-param name="c_name" select="concat('grouping_constr_', $id_com, '_max')"/></xsl:call-template> : <xsl:for-each select="key('offer_key', $id_go)/m3:groups[m3:groupedCommodity/@ref = $id_com]">
						<xsl:call-template name="coefficient">
							<xsl:with-param name="number" select="m3:groupedOffer/@coefficientValue"/>
						</xsl:call-template>*offer[<xsl:call-template name="single_quote"/><xsl:value-of select="m3:groupedOffer/@ref"/><xsl:call-template name="single_quote"/>] </xsl:for-each> &lt;= <xsl:value-of select="key('comm_key', $id_com)/@maxBalance"/>;<xsl:call-template name="newline"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>

	<!-- grouping constraints definition - with time -->	
	<xsl:template name="grouping_constraints_time">
		<xsl:for-each select="//m3:offers/m3:GroupingOffer">
			<xsl:variable name="id_go" select="@id"/>
	
			<xsl:for-each select="key('comm_key', key('offer_key', $id_go)/m3:groups/m3:groupedCommodity/@ref)">
				<xsl:variable name="id_com" select="@id"/>
				<!-- minimum balance -->
				<xsl:if test="key('comm_key', $id_com)/@minBalance != '-INF'">
					subject to <xsl:call-template name="constr_name"><xsl:with-param name="c_name" select="concat('grouping_constr_', $id_com, '_min')"/></xsl:call-template> : <xsl:for-each select="key('offer_key', $id_go)/m3:groups[m3:groupedCommodity/@ref = $id_com]">
						<xsl:call-template name="coefficient">
							<xsl:with-param name="number" select="m3:groupedOffer/@coefficientValue"/>
						</xsl:call-template>*offer[<xsl:call-template name="single_quote"/><xsl:value-of select="m3:groupedOffer/@ref"/><xsl:call-template name="single_quote"/>, <xsl:call-template name="single_quote"/><xsl:call-template name="offer_to_time"><xsl:with-param name="offer_id" select="m3:groupedOffer/@ref"/></xsl:call-template><xsl:call-template name="single_quote"/>] </xsl:for-each> &gt;= <xsl:value-of select="key('comm_key', $id_com)/@minBalance"/>;<xsl:call-template name="newline"/>
				</xsl:if>
				<!-- maximum balance -->
				<xsl:if test="key('comm_key', $id_com)/@maxBalance != '+INF'">
					subject to <xsl:call-template name="constr_name"><xsl:with-param name="c_name" select="concat('grouping_constr_', $id_com, '_max')"/></xsl:call-template> : <xsl:for-each select="key('offer_key', $id_go)/m3:groups[m3:groupedCommodity/@ref = $id_com]">
						<xsl:call-template name="coefficient">
							<xsl:with-param name="number" select="m3:groupedOffer/@coefficientValue"/>
						</xsl:call-template>*offer[<xsl:call-template name="single_quote"/><xsl:value-of select="m3:groupedOffer/@ref"/><xsl:call-template name="single_quote"/>] </xsl:for-each> &lt;= <xsl:value-of select="key('comm_key', $id_com)/@maxBalance"/>;<xsl:call-template name="newline"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>

	<!-- parameter generic declaration set -->
	<!-- $param_name - name of the param -->
	<!-- $set_name   - name of the set to which belongs the parameter -->
	<xsl:template name="parameter_generic_declaration_set">
		<xsl:param name="param_name"/>
		<xsl:param name="set_name"/>	
		<xsl:param name="postfix"></xsl:param>
		param <xsl:value-of select="$param_name"/>{<xsl:value-of select="$set_name"/>}<xsl:value-of select="$postfix"/>;<xsl:call-template name="newline"/>
	</xsl:template>
	
	<!-- generic parameters -->
	<!-- $dimension      - dimension of the parameter -->
	<!-- $param_name     - name of the parameter -->
	<!-- $param_set      - set to which the parameter belongs -->
	<!-- $param_xpath    - name of the (node|attribute|parameter) in which will be searching the parameters value -->
	<!-- $param_template - when parameter has its own template - use this parameter -->
	<xsl:template name="generic_parameters">
		<xsl:param name="dimension"/>
		<xsl:param name="param_name"/>
		<xsl:param name="param_set"></xsl:param>
		<xsl:param name="param_xpath"></xsl:param>
		<xsl:param name="param_template"></xsl:param>
		param <xsl:value-of select="$param_name"/> :<xsl:if test="$dimension = 1">=</xsl:if><xsl:call-template name="newline"/>
		<xsl:choose>
			<!-- with xpath -->
			<xsl:when test="$param_xpath != ''">
				<xsl:call-template name="parameter_generic_set">
					<xsl:with-param name="param_xpath" select="$param_xpath"/>
					<xsl:with-param name="param_set" select="$param_set"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$param_template != ''">
				<xsl:call-template name="additional_params">
					<xsl:with-param name="choose" select="$param_template"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
			</xsl:otherwise>
		</xsl:choose>
		;<xsl:call-template name="newline"/>			
	</xsl:template>

	<!-- parameter generic set -->
	<!-- $param_set      - set to which the parameter belongs -->
	<!-- $param_xpath    - name of the (node|attribute|parameter) in which will be searching the parameters value -->
	<xsl:template name="parameter_generic_set">
		<xsl:param name="param_xpath"/>
		<xsl:param name="param_set"/>
		<xsl:choose>
			<xsl:when test="$param_set = 'm3:Offer'">
				<xsl:for-each select="m3:offers/m3:Offer">
					<xsl:if test="count(.//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]) &gt; 0">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
						<xsl:value-of select=".//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]"/>
						<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$param_set = 'm3:MarketEntity'">
				<xsl:for-each select="m3:marketEntities/m3:MarketEntity">
					<xsl:if test="count(.//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]) &gt; 0">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
						<xsl:value-of select=".//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]"/>
						<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$param_set = 'm3:arc'">
				<xsl:for-each select="m3:networks/m3:Network/m3:arc">
					<xsl:if test="count(.//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]) &gt; 0">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
						<xsl:value-of select=".//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]"/>
						<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$param_set = 'm3:node'">
				<xsl:for-each select="m3:networks/m3:Network/m3:node">
					<xsl:if test="count(.//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]) &gt; 0">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
						<xsl:value-of select=".//@*[name() = $param_xpath] | .//*[name() = $param_xpath] | .//m3:parameter[@dref = $param_xpath]"/>
						<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<!-- additional parameters - non-standard parameter definition -->
	<!-- $choose - name of the template -->
	<!-- $add_par1   - additional parameter to be use in particular way -->
	<xsl:template name="additional_params">
		<xsl:param name="choose"/>
		<xsl:param name="add_par1"></xsl:param>
		<xsl:choose>
			<!-- sign -->
			<xsl:when test="$choose = 'sign_param'">
				<xsl:for-each select="//m3:offers/m3:Offer">
					<xsl:apply-templates select="." mode="set"/>
					<xsl:call-template name="tab"/>
					<xsl:apply-templates select="." mode="sign"/>1<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- share -->
			<xsl:when test="$choose = 'share_param'">
				<xsl:call-template name="tab"/>
				<xsl:for-each select="//m3:commodities/m3:Commodity">
					<xsl:if test="boolean(m3:VirtualCommodity) = false()">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
					</xsl:if>
				</xsl:for-each>:=
				<xsl:call-template name="newline"/>
				<xsl:for-each select="//m3:offers/m3:Offer">
					<xsl:variable name="id_offer" select="@id"/>
					<xsl:apply-templates select="." mode="set"/><xsl:call-template name="tab"/>
					<xsl:for-each select="//m3:commodities/m3:Commodity">
						<xsl:if test="boolean(m3:VirtualCommodity) = false()">
							<xsl:variable name="id_comm" select="@id"/>
							<xsl:choose>
								<xsl:when test="key('offer_key', $id_offer)/*/m3:offeredCommodity[@ref = $id_comm]">
									<xsl:value-of select="key('offer_key', $id_offer)/*/m3:offeredCommodity[@ref = $id_comm]/@shareFactor"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:text>0</xsl:text>
								</xsl:otherwise>
							</xsl:choose>
							<xsl:call-template name="tab"/>
						</xsl:if>
					</xsl:for-each>
					<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- price -->
			<xsl:when test="$choose = 'price'">
				<xsl:for-each select="m3:offers/m3:Offer">
					<xsl:apply-templates select="." mode="set"/>
					<xsl:call-template name="tab"/>
					<xsl:apply-templates select="." mode="inverseSign"/><xsl:value-of select="@offeredPrice"/>
					<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- price with calendat -->
			<xsl:when test="$choose = 'price_calendar'">
				<xsl:for-each select="m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="m3:offers/m3:Offer">
					<xsl:variable name="id_off" select="@id"/>
					<xsl:apply-templates select="key('offer_key', $id_off)" mode="set"/>
					<xsl:for-each select="//m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
						<xsl:variable name="id_cal" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="translate(key('cal_key', $id_cal)/@startTime, ':-T', '') &gt;= translate(key('offer_key', $id_off)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@startTime, ':-T', '') and translate(key('cal_key', $id_cal)/@endTime, ':-T', '') &lt;= translate(key('offer_key', $id_off)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@endTime, ':-T', '')"><xsl:apply-templates select="." mode="inverseSign"/><xsl:value-of select="key('offer_key', $id_off)/@offeredPrice"/></xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- programme with calendat -->
			<xsl:when test="$choose = 'programme_time'">
				<xsl:for-each select="m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="m3:marketEntities/m3:MarketEntity">
					<xsl:variable name="id_ent" select="@id"/>
					<xsl:apply-templates select="key('ent_key', $id_ent)" mode="set"/>
					<xsl:for-each select="//m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
						<xsl:variable name="id_cal" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:value-of select="sum(//m3:programmes/m3:Programme[@programmeType = 'm3:input-programme' and m3:programmeMarketEntity/@ref = $id_ent and key('comm_key', m3:programmeCommodity/@ref)/m3:CalendarScheduledCommodity/@ref = $id_cal]/@volume)"/>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- programme -->
			<xsl:when test="$choose = 'programme'">
				<xsl:for-each select="m3:marketEntities/m3:MarketEntity">
					<xsl:variable name="id_ent" select="@id"/>
					<xsl:apply-templates select="key('ent_key', $id_ent)" mode="set"/><xsl:call-template name="tab"/>
						<xsl:value-of select="sum(//m3:programmes/m3:Programme[@programmeType = 'm3:input-programme' and m3:programmeMarketEntity/@ref = $id_ent]/@volume)"/><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- programme -->
			<xsl:when test="$choose = 'programme_prog'">
				<xsl:for-each select="m3:programmes/m3:Programme[@programmeType = 'm3:input-programme']">
					<xsl:variable name="id_pr" select="@id"/>
					<xsl:apply-templates select="." mode="set"/><xsl:call-template name="tab"/><xsl:value-of select="@volume"/><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>

			<!-- offer lower bound with calendar -->
			<xsl:when test="$choose = 'offer_lower_bound_calendar'">
				<xsl:for-each select="m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="m3:offers/m3:Offer">
					<xsl:variable name="id_off" select="@id"/>
					<xsl:apply-templates select="key('offer_key', $id_off)" mode="set"/>
					<xsl:for-each select="//m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
						<xsl:variable name="id_cal" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="translate(key('cal_key', $id_cal)/@startTime, ':-T', '') &gt;= translate(key('offer_key', $id_off)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@startTime, ':-T', '') and translate(key('cal_key', $id_cal)/@endTime, ':-T', '') &lt;= translate(key('offer_key', $id_off)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@endTime, ':-T', '')"><xsl:apply-templates select="." mode="inverseSign"/><xsl:value-of select="key('offer_key', $id_off)/m3:volumeRange/@minValue"/></xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- offer upper bound with calendar -->
			<xsl:when test="$choose = 'offer_upper_bound_calendar'">
				<xsl:for-each select="m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="m3:offers/m3:Offer">
					<xsl:variable name="id_off" select="@id"/>
					<xsl:apply-templates select="key('offer_key', $id_off)" mode="set"/>
					<xsl:for-each select="//m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
						<xsl:variable name="id_cal" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="translate(key('cal_key', $id_cal)/@startTime, ':-T', '') &gt;= translate(key('offer_key', $id_off)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@startTime, ':-T', '') and translate(key('cal_key', $id_cal)/@endTime, ':-T', '') &lt;= translate(key('offer_key', $id_off)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@endTime, ':-T', '')"><xsl:apply-templates select="." mode="inverseSign"/><xsl:value-of select="key('offer_key', $id_off)/m3:volumeRange/@maxValue"/></xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>			
			<!-- network_flow -->
			<xsl:when test="$choose = 'paths_flow'">
				<xsl:for-each select="m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:node">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="//m3:offers/m3:Offer[*/m3:offeredCommodity/@ref = key('comm_dref', 'ex:bandwidth-trade-path')/@id]">
					<xsl:variable name="id_off" select="@id"/>
					<xsl:apply-templates select="key('offer_key', $id_off)" mode="set"/>
					<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:node">
						<xsl:variable name="id_node" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="key('node_key', key('arc_key', key('comm_key', key('offer_key', $id_off)/*/m3:offeredCommodity/@ref)/m3:availableAt/@ref)/m3:predecessor/@ref)/m3:aggregates/@ref = $id_node">1</xsl:when>
							<xsl:when test="key('node_key', key('arc_key', key('comm_key', key('offer_key', $id_off)/*/m3:offeredCommodity/@ref)/m3:availableAt/@ref)/m3:successor/@ref)/m3:aggregates/@ref = $id_node">-1</xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>			
			<!-- network_flow -->
			<xsl:when test="$choose = 'links_flow'">
				<xsl:for-each select="m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:node">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="//m3:offers/m3:Offer[*/m3:offeredCommodity/@ref = key('comm_dref', 'ex:bandwidth-trade-link')/@id]">
					<xsl:variable name="id_off" select="@id"/>
					<xsl:apply-templates select="key('offer_key', $id_off)" mode="set"/>
					<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:node">
						<xsl:variable name="id_node" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="key('arc_key', key('comm_key', key('offer_key', $id_off)/*/m3:offeredCommodity/@ref)/m3:availableAt/@ref)/m3:predecessor/@ref = $id_node">1</xsl:when>
							<xsl:when test="key('arc_key', key('comm_key', key('offer_key', $id_off)/*/m3:offeredCommodity/@ref)/m3:availableAt/@ref)/m3:successor/@ref = $id_node">-1</xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>			
			<!-- network_flow -->
			<xsl:when test="$choose = 'links_flow_ev'">
				<xsl:for-each select="m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:node">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:arc">
					<xsl:variable name="id_edge" select="@id"/>
					<xsl:apply-templates select="key('arc_key', $id_edge)" mode="set"/>
					<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:node">
						<xsl:variable name="id_node" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="key('arc_key', $id_edge)/m3:predecessor/@ref = $id_node">1</xsl:when>
							<xsl:when test="key('arc_key', $id_edge)/m3:successor/@ref = $id_node">-1</xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>			
			<!-- network_flow -->
			<xsl:when test="$choose = 'paths_flow_ev'">
				<xsl:for-each select="key('node_key', m3:networks/m3:Network[boolean(m3:VirtualNetwork) = true()]/m3:node/m3:aggregates/@ref)">
					<xsl:call-template name="tab"/><xsl:apply-templates select="." mode="set"/>
				</xsl:for-each>:=<xsl:call-template name="newline"/>
				<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = true()]/m3:arc">
					<xsl:variable name="id_edge" select="@id"/>
					<xsl:apply-templates select="key('arc_key', $id_edge)" mode="set"/>
					<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = true()]/m3:node">
						<xsl:variable name="id_node" select="@id"/>
						<xsl:call-template name="tab"/>
						<xsl:choose>
							<xsl:when test="key('arc_key', $id_edge)/m3:predecessor/@ref = $id_node">1</xsl:when>
							<xsl:when test="key('arc_key', $id_edge)/m3:successor/@ref = $id_node">-1</xsl:when>
							<xsl:otherwise>0</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>			
			<!-- min_balance -->
			<xsl:when test="$choose = 'min_balance'">
				<xsl:for-each select="//m3:commodities/m3:Commodity[boolean(m3:VirtualCommodity) = false() and boolean(@minBalance) = true()]"><xsl:apply-templates select="key('comm_key', @id)" mode="set"/><xsl:call-template name="tab"/><xsl:choose><xsl:when test="@minBalance = 'INF'">10e12</xsl:when><xsl:when test="@minBalance = '-INF'">-10e12</xsl:when><xsl:otherwise><xsl:value-of select="@minBalance"/></xsl:otherwise></xsl:choose><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- max_balance -->
			<xsl:when test="$choose = 'max_balance'">
				<xsl:for-each select="//m3:commodities/m3:Commodity[boolean(m3:VirtualCommodity) = false() and boolean(@maxBalance) = true()]"><xsl:apply-templates select="key('comm_key', @id)" mode="set"/><xsl:call-template name="tab"/><xsl:choose><xsl:when test="@maxBalance = 'INF'">10e12</xsl:when><xsl:when test="@maxBalance = '-INF'">-10e12</xsl:when><xsl:otherwise><xsl:value-of select="@maxBalance"/></xsl:otherwise></xsl:choose><xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'commodity_periods'">
				<xsl:for-each select="//m3:commodities/m3:Commodity">
					<xsl:if test="boolean(m3:VirtualCommodity) = false()">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
						<xsl:for-each select="m3:CalendarScheduledCommodity">
							<xsl:call-template name="single_quote"/> <xsl:value-of select="@ref"/> <xsl:call-template name="single_quote"/>
						</xsl:for-each>
						<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'commodity_kinds'">
				<xsl:for-each select="//m3:commodities/m3:Commodity">
					<xsl:if test="boolean(m3:VirtualCommodity) = false()">
						<xsl:apply-templates select="." mode="set"/>
						<xsl:call-template name="tab"/>
						<xsl:call-template name="single_quote"/> <xsl:value-of select="@dref"/> <xsl:call-template name="single_quote"/>
						<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'commodity_nodes'">
				<xsl:for-each select="//m3:commodities/m3:Commodity">
					<xsl:if test="boolean(m3:VirtualCommodity) = false()">
					<xsl:apply-templates select="." mode="set"/>
					<xsl:call-template name="tab"/>
					<xsl:call-template name="single_quote"/> <xsl:value-of select="m3:availableAt/@ref"/> <xsl:call-template name="single_quote"/>
					<xsl:call-template name="newline"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'reserve_direction'">
				<xsl:for-each select="//m3:Commodity[@dref='op:Reserve']">
					<xsl:apply-templates select="." mode="set"/>
					<xsl:call-template name="tab"/>
					<xsl:call-template name="single_quote"/><xsl:value-of select="m3:parameter[@dref='op:reserveDirection']"/><xsl:call-template name="single_quote"/>
					<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'reserve_type'">
				<xsl:for-each select="//m3:Commodity[@dref='op:Reserve']">
					<xsl:apply-templates select="." mode="set"/>
					<xsl:call-template name="tab"/>
					<xsl:call-template name="single_quote"/><xsl:value-of select="m3:parameter[@dref='op:reserveActivationType']"/><xsl:call-template name="single_quote"/>
					<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'to_node'">
				<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:arc"><xsl:apply-templates select="." mode="set"/><xsl:call-template name="tab"/><xsl:apply-templates select="key('node_key', m3:successor/@ref)" mode="set"/>
					<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$choose = 'from_node'">
				<xsl:for-each select="//m3:networks/m3:Network[boolean(m3:VirtualNetwork) = false()]/m3:arc"><xsl:apply-templates select="." mode="set"/><xsl:call-template name="tab"/><xsl:apply-templates select="key('node_key', m3:predecessor/@ref)" mode="set"/>
					<xsl:call-template name="newline"/>
				</xsl:for-each>
			</xsl:when>
			<!-- other parameters -->
			<xsl:otherwise>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	
	<!-- naming -->
	<xsl:template match="m3:Offer|m3:Commodity|m3:arc|m3:node|m3:CalendarPeriod|m3:MarketEntity|m3:Programme" mode="name">
		<xsl:value-of select="@id"/>
	</xsl:template>
	
	<!-- utilities -->
	<xsl:template name="newline">
		<xsl:text>&#10;</xsl:text>
	</xsl:template>
	
	<xsl:template name="tab">
		<xsl:text>&#09;</xsl:text>
	</xsl:template>
	
	<xsl:template name="single_quote">
		<xsl:text>&#39;</xsl:text>
	</xsl:template>
	
	<xsl:template name="constr_name">
		<xsl:param name="c_name"/>
		<xsl:value-of select="translate($c_name, ':-', '')"/>
	</xsl:template>
	
	<!-- sign -->
	
	<xsl:template match="m3:Offer" mode="sign">
		<xsl:choose>
			<xsl:when test="m3:ElementaryOffer/m3:offeredCommodity[@shareFactor = '1']">
				<xsl:text></xsl:text>
			</xsl:when>
			<!-- sam nie wiem czy tak ma byc ?? -->
			<xsl:when test="m3:BundledOffer">
				<xsl:text></xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>-</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="m3:Offer" mode="inverseSign">
<!--		<xsl:choose>
			<xsl:when test="m3:ElementaryOffer/m3:offeredCommodity[@shareFactor = '-1']">
				<xsl:text></xsl:text>
			</xsl:when>
			
			<xsl:when test="m3:BundledOffer">
				<xsl:text>-</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>-</xsl:text>
			</xsl:otherwise>
		</xsl:choose> -->
	</xsl:template>
	
	<xsl:template name="coefficient">
		<xsl:param name="number"/>
		<xsl:if test="number($number) &gt;= 0">+</xsl:if><xsl:value-of select="$number"/>
	</xsl:template>
	
	<xsl:template name="inverse_sign">
		<xsl:param name="number"/>
		<xsl:value-of select="$number * -1"/>
	</xsl:template>
	
	<xsl:template name="offer_to_time">
		<xsl:param name="offer_id"/>
		<xsl:for-each select="//m3:calendar/m3:CalendarPeriod[boolean(m3:aggregates) = false()]">
			<xsl:variable name="id_cal" select="@id"/>
			<xsl:if test="translate(key('cal_key', $id_cal)/@startTime, ':-T', '') &lt;= translate(key('offer_key', $offer_id)/m3:offerStatus[@status = 'm3:offer-open']/m3:durationPeriod/@startTime, ':-T', '') "><xsl:value-of select="$id_cal"/></xsl:if>
		</xsl:for-each>
	</xsl:template>

</xsl:stylesheet>